#!/bin/bash

################基础配置参数，需要模型审视修改##################
# 必选字段(必须在此处定义的参数): Network batch_size RANK_SIZE
# 网络名称，同目录名称
Network="Pix2PixHD"
# 训练batch_size
batch_size=1
# 训练使用的npu卡数
export RANK_SIZE=8
# 数据集路径,保持为空,不需要修改
data_path=""
# 设定训练的阶段
export stage=1

# 训练epoch
train_epochs=200
# 学习率
learning_rate=0.0002
# 加载数据进程数
workers=8


# 参数校验，data_path为必传参数，其他参数的增删由模型自身决定；此处新增参数需在上面有定义并赋值
for para in $*
do
    if [[ $para == --workers* ]];then
        workers=`echo ${para#*=}`
    elif [[ $para == --data_path* ]];then
        data_path=`echo ${para#*=}`
    fi
done

# 校验是否传入data_path,不需要修改
if [[ $data_path == "" ]];then
    echo "[Error] para \"data_path\" must be confing"
    exit 1
fi


###############指定训练脚本执行路径###############
# cd到与test文件夹同层级目录下执行脚本，提高兼容性；test_path_dir为包含test文件夹的路径
cur_path=`pwd`
cur_path_last_dirname=${cur_path##*/}
if [ x"${cur_path_last_dirname}" == x"test" ];then
    test_path_dir=${cur_path}
    cd ..
    cur_path=`pwd`
else
    test_path_dir=${cur_path}/test
fi


#################创建日志输出目录，不需要修改#################
ASCEND_DEVICE_ID=0
if [ -d ${test_path_dir}/output/${ASCEND_DEVICE_ID} ];then
    rm -rf ${test_path_dir}/output/${ASCEND_DEVICE_ID}
    mkdir -p ${test_path_dir}/output/$ASCEND_DEVICE_ID
else
    mkdir -p ${test_path_dir}/output/$ASCEND_DEVICE_ID
fi


#################启动训练脚本#################
#训练开始时间，不需要修改
start_time=$(date +%s)
# 非平台场景时source 环境变量
check_etp_flag=`env | grep etp_running_flag`
etp_flag=`echo ${check_etp_flag#*=}`
if [ x"${etp_flag}" != x"true" ];then
    source ${test_path_dir}/env_npu.sh
fi

# 启动第一阶段的训练
for((RANK_ID=0;RANK_ID<RANK_SIZE;RANK_ID++));
do
    export RANK_ID=$RANK_ID
    start=$((24 * RANK_ID))
    end=$((start + 23))
    taskset -c $start-$end nohup python -u ./train.py \
        --seed 1234 \
        --local_rank 0 \
        --fp16 \
        --name label2city_512p \
        --batchSize ${batch_size} \
        --num_workers ${workers} \
        --lr ${learning_rate} \
        --node_device "npu"  > ${test_path_dir}/output/${ASCEND_DEVICE_ID}/first_stage_train_${ASCEND_DEVICE_ID}.log 2>&1 &

done

wait

# 改变状态的指示
export stage=2
# 第一阶段训练完成，启动第二阶段的训练
for((RANK_ID=0;RANK_ID<RANK_SIZE;RANK_ID++));
do
    export RANK_ID=$RANK_ID
    start=$((24 * RANK_ID))
    end=$((start + 23))
    taskset -c $start-$end nohup python -u ./train.py \
        --seed 1234 \
        --resize_or_crop none \
        --local_rank 0 \
        --fp16 \
        --load_pretrain checkpoints/label2city_512p \
        --name label2city_1024p \
        --netG local \
        --ngf 32 \
        --num_D 3 \
        --niter 50 \
        --niter_decay 50 \
        --niter_fix_global 10 \
        --batchSize ${batch_size} \
        --num_workers ${workers} \
        --lr ${learning_rate} \
        --node_device "npu"  > ${test_path_dir}/output/${ASCEND_DEVICE_ID}/second_stage_train_${ASCEND_DEVICE_ID}.log 2>&1 &

done

wait


##################获取训练数据################
#训练结束时间，不需要修改
end_time=$(date +%s)
e2e_time=$(( $end_time - $start_time ))

#结果打印，不需要修改
echo "------------------ Final result ------------------"
#输出性能FPS，需要模型审视修改
Step_CostTime=`grep -a 'epoch:'  ${test_path_dir}/output/${ASCEND_DEVICE_ID}/second_stage_train_${ASCEND_DEVICE_ID}.log|awk -F " " '{print $6}' | awk 'END {print}' |awk -F ")" '{print $1}'`
#打印，不需要修改
FPS=`awk 'BEGIN{printf "%.2f\n",8/'${Step_CostTime}'}'`
echo "Final Performance images/sec : $FPS"

#输出训练精度,需要模型审视修改
train_accuracy=0
#打印，不需要修改
echo "Final Train Accuracy : ${train_accuracy}"
echo "E2E Training Duration sec : $e2e_time"

#性能看护结果汇总
#训练用例信息，不需要修改
BatchSize=${batch_size}
DeviceType=`uname -m`
CaseName=${Network}_bs${BatchSize}_${RANK_SIZE}'p'_'acc'

##获取性能数据，不需要修改
#吞吐量
ActualFPS=${FPS}
#单迭代训练时长
TrainingTime=`awk 'BEGIN{printf "%.2f\n", '${batch_size}'*1000/'${FPS}'}'`

#从train_$ASCEND_DEVICE_ID.log提取Loss到train_${CaseName}_loss.txt中，需要根据模型审视
grep D_fake ${test_path_dir}/output/$ASCEND_DEVICE_ID/second_stage_train_${ASCEND_DEVICE_ID}.log|awk -F "D_fake=" '{print $NF}' | awk -F ")" '{print $2}' >>  ${test_path_dir}/output/$ASCEND_DEVICE_ID/train_${CaseName}_loss.txt

#最后一个迭代loss值，不需要修改
# 下面的这一句的直接意思是输出文件的最后一行
ActualLoss=`awk 'END {print}'  ${test_path_dir}/output/$ASCEND_DEVICE_ID/train_${CaseName}_loss.txt`

#关键信息打印到${CaseName}.log中，不需要修改
echo "Network = ${Network}" >  ${test_path_dir}/output/$ASCEND_DEVICE_ID/${CaseName}.log
echo "RankSize = ${RANK_SIZE}" >>  ${test_path_dir}/output/$ASCEND_DEVICE_ID/${CaseName}.log
echo "BatchSize = ${BatchSize}" >>  ${test_path_dir}/output/$ASCEND_DEVICE_ID/${CaseName}.log
echo "DeviceType = ${DeviceType}" >>  ${test_path_dir}/output/$ASCEND_DEVICE_ID/${CaseName}.log
echo "CaseName = ${CaseName}" >>  ${test_path_dir}/output/$ASCEND_DEVICE_ID/${CaseName}.log
echo "ActualFPS = ${ActualFPS}" >>  ${test_path_dir}/output/$ASCEND_DEVICE_ID/${CaseName}.log
echo "TrainingTime = ${TrainingTime}" >>  ${test_path_dir}/output/$ASCEND_DEVICE_ID/${CaseName}.log
echo "TrainAccuracy = ${train_accuracy}" >> ${test_path_dir}/output/$ASCEND_DEVICE_ID/${CaseName}.log
echo "ActualLoss = ${ActualLoss}" >>  ${test_path_dir}/output/$ASCEND_DEVICE_ID/${CaseName}.log
echo "E2ETrainingTime = ${e2e_time}" >>  ${test_path_dir}/output/$ASCEND_DEVICE_ID/${CaseName}.log
